import sequelize from "../../common/db";
import { DataTypes, Model } from "sequelize";

class RoleModel extends Model {
  declare id: number;
  declare users: string;
  declare status: number;
  declare buttons?: string[];
  declare menus?: string[];
  declare name: string;
  declare remark?: string;
}
// 创建模型
RoleModel.init(
  {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    name: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    buttons: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    users: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    status: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      validate: {
        isIn: {
          args: [[0, 1]],
          msg: "状态只能是0或1",
        },
      },
    },
    menus: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    remark: {
      type: DataTypes.STRING,
      allowNull: true,
    },
  },
  {
    sequelize,
    freezeTableName: true, // 禁止执行推导复数
    tableName: "role", // 角色表
    timestamps: true, // 自动生成时间戳
    updatedAt: "update_time", // 更新时间
    createdAt: "create_time", // 创建时间
  },
);

export default RoleModel;
